import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart';
import 'package:untitled/res_entity.dart';

Dio dio = Dio();
void main() {
  runApp(GetMaterialApp(home: Home()));
}

class Controller extends GetxController {
  var city = ''.obs;
}

class Home extends StatelessWidget {
  final Controller c = Get.put(Controller());
  final _longitudeController = TextEditingController(text: "104.438501");
  final _latitudeController = TextEditingController(text: "37.877405");

  Widget build(context) {
    return Scaffold(
      appBar: AppBar(title: Obx(() => Text("城市: ${c.city}"))),
      body: SafeArea(
        child: ListView(
          children: <Widget>[
            TextField(
              controller: _longitudeController,
              decoration: InputDecoration(
                filled: true,
                labelText: '经度',
              ),
            ),
            TextField(
              controller: _latitudeController,
              decoration: InputDecoration(
                filled: true,
                labelText: '纬度',
              ),
            ),
            ButtonBar(
              children: <Widget>[
                TextButton(
                  style: TextButton.styleFrom(
                    textStyle: const TextStyle(fontSize: 20),
                  ),
                  onPressed: () async {
                    try {
                      var response = await Dio().get(
                          "http://localhost:4500/zhanghengxin/locations/resolvecity",
                          options: Options(),
                          queryParameters: {
                            'longitude': _longitudeController.text,
                            'latitude': _latitudeController.text
                          });
                      if (response.statusCode == 200) {
                        ResEntity jsondata = ResEntity.fromJson(response.data);
                        c.city.value = jsondata.location.city;
                      }
                    } catch (e) {
                      print(e);
                    }
                  },
                  child: const Text('请求'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

